﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

/// <summary>
/// Summary description for Utilities
/// </summary>
public static class Utilities
{
    public static void LoadPage(long langid, string page, out string text, out string keywords)
    {
        long lang = langid;
        text = "";
        keywords = "";
        if (lang != -1 && page != "-1")
        {
            using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetPageByName(@lang_id,@page)", conn))
                {
                    SqlDataReader dr = null;
                    SqlParameter[] pars = new SqlParameter[2];
                    pars[0] = new SqlParameter("lang_id", lang);
                    pars[1] = new SqlParameter("page", page);
                    cmd.Parameters.Add(pars[0]);
                    cmd.Parameters.Add(pars[1]);

                    dr = cmd.ExecuteReader();
                    if (dr != null)
                    {
                        if (dr.HasRows)
                        {
                            if (dr.Read())
                            {
                                text = dr["html"].ToString();
                                keywords = dr["keywords"].ToString();
                            }
                        }
                    }
                    dr.Close();
                }
            }
        }

    }

    public static bool IsValidEmail(string email)
    {
        Regex rx = new Regex(
        @"^[-!#$%&'*+/0-9=?A-Z^_a-z{|}~](\.?[-!#$%&'*+/0-9=?A-Z^_a-z{|}~])*@[a-zA-Z](-?[a-zA-Z0-9])*(\.[a-zA-Z](-?[a-zA-Z0-9])*)+$");
        if (email != null)
            return rx.IsMatch(email);
        else return false;
    }


    public static void SetLabel(MasterPage m, string s)
    {
        Label l =
            (Label)m.FindControl("lblTitle");
        if (l != null)
        {
            l.Text = s;
        }
    }
    public static DateTime GetStartOfLastWeek()
    {
        int DaysToSubtract = (int)DateTime.Now.DayOfWeek + 7;
        DateTime dt = DateTime.Now.Subtract(TimeSpan.FromDays(DaysToSubtract));
        return new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0, 0);
    }
}
